home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / StringBufferInputStream.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.8 KB  |  162 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)StringBufferInputStream.java    1.20 98/06/29
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. /**
  18.  * This class allows an application to create an input stream in
  19.  * which the bytes read are supplied by the contents of a string.
  20.  * Applications can also read bytes from a byte array by using a
  21.  * <code>ByteArrayInputStream</code>.
  22.  * <p>
  23.  * Only the low eight bits of each character in the string are used by
  24.  * this class.
  25.  *
  26.  * @author     Arthur van Hoff
  27.  * @version    1.20, 06/29/98
  28.  * @see        java.io.ByteArrayInputStream
  29.  * @see        java.io.StringReader
  30.  * @since      JDK1.0
  31.  * @deprecated This class does not properly convert characters into bytes.  As
  32.  *             of JDK 1.1, the preferred way to create a stream from a
  33.  *             string is via the <code>StringReader</code> class.
  34.  */
  35. public
  36. class StringBufferInputStream extends InputStream {
  37.     /**
  38.      * The string from which bytes are read.
  39.      */
  40.     protected String buffer;
  41.  
  42.     /**
  43.      * The index of the next character to read from the input stream buffer.
  44.      *
  45.      * @see        java.io.StringBufferInputStream#buffer
  46.      */
  47.     protected int pos;
  48.  
  49.     /**
  50.      * The number of valid characters in the input stream buffer.
  51.      *
  52.      * @see        java.io.StringBufferInputStream#buffer
  53.      */
  54.     protected int count;
  55.  
  56.     /**
  57.      * Creates a string input stream to read data from the specified string.
  58.      *
  59.      * @param      s   the underlying input buffer.
  60.      */
  61.     public StringBufferInputStream(String s) {
  62.     this.buffer = s;
  63.     count = s.length();
  64.     }
  65.  
  66.     /**
  67.      * Reads the next byte of data from this input stream. The value
  68.      * byte is returned as an <code>int</code> in the range
  69.      * <code>0</code> to <code>255</code>. If no byte is available
  70.      * because the end of the stream has been reached, the value
  71.      * <code>-1</code> is returned.
  72.      * <p>
  73.      * The <code>read</code> method of
  74.      * <code>StringBufferInputStream</code> cannot block. It returns the
  75.      * low eight bits of the next character in this input stream's buffer.
  76.      *
  77.      * @return     the next byte of data, or <code>-1</code> if the end of the
  78.      *             stream is reached.
  79.      */
  80.     public synchronized int read() {
  81.     return (pos < count) ? (buffer.charAt(pos++) & 0xFF) : -1;
  82.     }
  83.  
  84.     /**
  85.      * Reads up to <code>len</code> bytes of data from this input stream
  86.      * into an array of bytes.
  87.      * <p>
  88.      * The <code>read</code> method of
  89.      * <code>StringBufferInputStream</code> cannot block. It copies the
  90.      * low eight bits from the characters in this input stream's buffer into
  91.      * the byte array argument.
  92.      *
  93.      * @param      b     the buffer into which the data is read.
  94.      * @param      off   the start offset of the data.
  95.      * @param      len   the maximum number of bytes read.
  96.      * @return     the total number of bytes read into the buffer, or
  97.      *             <code>-1</code> if there is no more data because the end of
  98.      *             the stream has been reached.
  99.      */
  100.     public synchronized int read(byte b[], int off, int len) {
  101.     if (b == null) {
  102.         throw new NullPointerException();
  103.     } else if ((off < 0) || (off > b.length) || (len < 0) ||
  104.            ((off + len) > b.length) || ((off + len) < 0)) {
  105.         throw new IndexOutOfBoundsException();
  106.     }
  107.     if (pos >= count) {
  108.         return -1;
  109.     }
  110.     if (pos + len > count) {
  111.         len = count - pos;
  112.     }
  113.     if (len <= 0) {
  114.         return 0;
  115.     }
  116.     String    s = buffer;
  117.     int cnt = len;
  118.     while (--cnt >= 0) {
  119.         b[off++] = (byte)s.charAt(pos++);
  120.     }
  121.  
  122.     return len;
  123.     }
  124.  
  125.     /**
  126.      * Skips <code>n</code> bytes of input from this input stream. Fewer
  127.      * bytes might be skipped if the end of the input stream is reached.
  128.      *
  129.      * @param      n   the number of bytes to be skipped.
  130.      * @return     the actual number of bytes skipped.
  131.      */
  132.     public synchronized long skip(long n) {
  133.     if (n < 0) {
  134.         return 0;
  135.     }
  136.     if (n > count - pos) {
  137.         n = count - pos;
  138.     }
  139.     pos += n;
  140.     return n;
  141.     }
  142.  
  143.     /**
  144.      * Returns the number of bytes that can be read from the input
  145.      * stream without blocking.
  146.      *
  147.      * @return     the value of <code>count - pos</code>, which is the
  148.      *             number of bytes remaining to be read from the input buffer.
  149.      */
  150.     public synchronized int available() {
  151.     return count - pos;
  152.     }
  153.  
  154.     /**
  155.      * Resets the input stream to begin reading from the first character
  156.      * of this input stream's underlying buffer.
  157.      */
  158.     public synchronized void reset() {
  159.     pos = 0;
  160.     }
  161. }
  162.